"use strict";

const Controller = require("egg").Controller;

class ContontController extends Controller {
    async check() {
        const res = await this.service.contont.check(); //定义查询
        this.ctx.body = {
            code: 200,
            data: res,
            msg: "查询成功",
        };
        return; //返回数据
    }
    //添加
    async add() {
        //解析请求体参数
        const { title, content } = this.ctx.request.body;
        //判断参数是否为空
        if (!title || !content) {
            this.ctx.body = {
                code: 401,
                msg: "参数为空",
            };
            return;
        }
        //添加一条数据
        //自己添加图片
        let img =
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2522246188,3430142227&fm=26&gp=0.jpg";
        let res = await this.service.contont.add(title, content, img);
        if (res.affectedRows === 1) {
            this.ctx.body = {
                code: 201,
                msg: "添加成功",
            };
        }
    }
    /**
     * 删除
     * id
     * 删除query接受参数
     */
    async remove() {
        // 接收路径参数
        const { id } = this.ctx.query;
        const res = await this.service.contont.remove(id);
        // 判断是否删除成功
        if (res.affectedRows === 1) {
            this.ctx.body = {
                code: 204,
                msg: "删除成功",
            };
        }
    }
    /**
     * id 数据库
     * title 标题
     * content 内容
     * 修改
     */
    async change() {
        //接受路径参数
        const { id, title, content } = this.ctx.request.body;
        console.log(id, title, content);
        const res = await this.service.contont.change(title, content, id);
        this.ctx.body = {
            code: 200,
            msg: "修改成功",
        };
    }
}

module.exports = ContontController;
